/*
 * removeItem.java
 *
 * Created on March 26, 2007, 7:11 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.actions;

import javax.servlet.http.*;
import edu.byu.isys413.group1E.data.*;
import edu.byu.isys413.group1E.handlers.*;
import java.util.ArrayList;

/**This class removes a Transaction line from the current Transaction, and removes it from the visible cart
 *
 * @author Kendall Frazier
 */
public class removeItem implements edu.byu.isys413.web.Action {
    
    /** 
     *Processes this method gets teh Transaction out of the session, and an ID from the cart2.jsp.  It removes the
     *transaction line with that id from the Transaction.  Then it reloads cart2.jsp
     *
     *@param HttpServletRequest
     *@param HttpServletResponse
     *
     *@return String the name of the jsp to call
     */
    public removeItem() {
    }
  
    /** Processes a number guess */
    public String process(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        PrintHandler pHandler = new PrintHandler();
        SaleHandler sHandler = SaleHandler.getInstance();
        TransactionDAO TXDAO = TransactionDAO.getInstance();
        TransLineDAO TLDAO = TransLineDAO.getInstance();
        
        //read in parameters and create sale RevSrc
        String id = request.getParameter("id");
                   
        //create & populate Transaction
        TransactionBO TXBO = null;
        if(session.getAttribute("TX")==null)
        {
            TXBO = TXDAO.create();
        }else{
            TXBO = (TransactionBO)session.getAttribute("TX");
        }
        
        TransLineBO TL = TLDAO.read(id);
        ArrayList<TransLineBO> TLList = TXBO.getTxLineList();
        for(int counter = 0; counter < TLList.size(); counter++){
            if(TLList.get(counter).getTxLineID().equals(TL.getTxLineID())){
                TLList.remove(counter);
            }            
        }
                
        TXBO.calcSubTotal();
        TXBO.calcTax();        
        TXBO.calcTotal();
        
        request.setAttribute("TX", TXBO);
                
        session.setAttribute("TX", TXBO);        
        return "cart2.jsp";
    }//process
    
    
}//removeItem class
